<template>
    <div class="chat-interface">
        <!-- 联系人头像和头衔区域 -->
        <div class="contact-info" v-if="currentContact">
            <img :src="currentContact.avatar" alt="Avatar" class="avatar" />
            <span class="contact-title">{{ currentContact.name }}</span>
        </div>
        <!-- 聊天消息显示区域 -->
        <div class="chat-messages" v-if="currentContact">
            <div
                v-for="(message, index) in currentContact.messages"
                :key="index"
                :class="['chat-message', { 'self-message': message.sender === 'you' }]"
            >
                <img
                    :src="message.sender === 'you'? 'https://picsum.photos/30/30' : currentContact.avatar"
                    alt="Avatar"
                    class="message-avatar"
                />
                <div class="message-content">
                    <span>{{ message.content }}</span>
                    <span class="message-time">{{ message.time }}</span>
                </div>
            </div>
        </div>
        <!-- 消息输入框和发送相关区域 -->
        <div class="message-input-area" v-if="currentContact">
            <!-- 表情图标和文件选择图标区域 -->
            <div class="input-icons">
                <i class="fa-regular fa-face-smile" @click="openEmojiPicker"></i>
                <i class="fa-regular fa-file" @click="openFilePicker"></i>
            </div>
            <!-- 消息输入框和发送按钮区域 -->
            <div class="input-and-send">
                <textarea
                    v-model="newMessage"
                    placeholder="输入消息..."
                    class="message-input"
                ></textarea>
                <button @click="sendMessage" class="send-button">发送</button>
            </div>
        </div>
        <div v-else class="no-contact-selected">请选择一个联系人开始聊天</div>
    </div>
</template>

<script>
export default {
    props: {
        currentContact: {
            type: Object,
            default: null
        }
    },
    data() {
        return {
            newMessage: ''
        };
    },
    methods: {
        sendMessage() {
            if (this.newMessage.trim()!== '') {
                const newMsg = {
                    sender: 'you',
                    content: this.newMessage,
                    time: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
                };
                this.currentContact.messages.push(newMsg);
                this.newMessage = '';
            }
        },
        openEmojiPicker() {
            console.log('打开表情包选择器');
        },
        openFilePicker() {
            this.$refs.fileInput.click();
        },
        sendFile() {
            const file = this.$refs.fileInput.files[0];
            if (file) {
                console.log('发送文件:', file.name);
                this.$refs.fileInput.value = '';
            }
        }
    }
};
</script>

<style scoped>
/* 设置所有元素使用 border-box 盒模型 */
*,
*::before,
*::after {
    box-sizing: border-box;
}
body {
    margin: 0;
    padding: 0;
}
.chat-interface {
    height: 100vh;
    display: flex;
    flex-direction: column;
    width: 100%; 
    padding: 0;
    overflow: hidden; 
}

.contact-info {
    display: flex;
    align-items: center;
    padding: 15px;
    border-bottom: 1px solid #e0e0e0;
    background-color: #f5f5f5;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
}

.contact-title {
    font-weight: bold;
    font-size: 18px;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    background-color: #f9f9f9;
}

.chat-message {
    margin-bottom: 10px;
    display: flex;
    align-items: flex-start;
}

.self-message {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    margin: 0 8px;
}

.message-content {
    background-color: #e0f7fa;
    padding: 8px 12px;
    border-radius: 8px;
    max-width: 70%; 
    position: relative;
    word-wrap: break-word;
    word-break: normal;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    box-sizing: border-box;
    max-inline-size: 100%;
    min-inline-size: 0;
}

.self-message .message-content {
    background-color: #c8e6c9;
}

.message-time {
    font-size: 10px;
    color: #777;
    position: absolute;
    bottom: -12px;
    right: 5px;
}

.message-input-area {
    display: flex;
    align-items: center;
    padding: 15px;
    border-top: 1px solid #e0e0e0;
    background-color: #f5f5f5;
}

.input-icons {
    display: flex;
    flex-direction: column;
    margin-right: 10px;
}

.input-and-send {
    flex: 1;
    display: flex;
}

.message-input {
    flex: 1;
    padding: 8px;
    border: 1px solid #e0e0e0;
    border-radius: 5px;
    margin-right: 10px;
    resize: none;
}

.send-button {
    padding: 8px 12px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

.send-button:hover {
    background-color: #0056b3;
}

.no-contact-selected {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #777;
}

/* 图标样式 */
.fa-regular {
    font-size: 20px;
    cursor: pointer;
    margin-bottom: 5px;
}
</style>    